package com.util;


import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.xiaochun.hxhj.LoginActivity;
import com.xiaochun.hxhj.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fei on 2017/6/20.
 */

public class ExitAppUtil {
    public Context context;
    public Activity activity;
    public View view;
    private PopupWindow mPopupWindowexit;
    private SharedPreferences sharedPreferences;

    private static ExitAppUtil singleton;

    private ExitAppUtil(){

    }

    public static synchronized ExitAppUtil getInstance(){//同步控制,避免多线程的状况多创建了实例对象
        if (singleton==null){
            singleton=new ExitAppUtil();//在需要的时候在创建
        }
        return singleton;
    }

    /**
     * 弹出退出页面搜索框
     */
    public void popbyexit(String str,Context context,Activity activity,View view) {
        sharedPreferences = context.getSharedPreferences("userinfo", Activity.MODE_PRIVATE);

        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View popupWindow = layoutInflater.inflate(R.layout.popup_exit_app, null);

        TextView tv_des = popupWindow.findViewById(R.id.tv_des);
        tv_des.setText(str);
        TextView tv_sure = popupWindow.findViewById(R.id.tv_sure);
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mPopupWindowexit.dismiss();
//                logoutUser();
                sharedPreferences.edit().putString("user_id","").commit();
                sharedPreferences.edit().putString("username","").commit();
                sharedPreferences.edit().putString("nickname","").commit();
                sharedPreferences.edit().putString("mobile","").commit();
                sharedPreferences.edit().putString("avatar","").commit();
                sharedPreferences.edit().putString("score","").commit();
                sharedPreferences.edit().putString("student_id","").commit();
                sharedPreferences.edit().putString("is_bind_wx","").commit();
                sharedPreferences.edit().putString("token","").commit();
                sharedPreferences.edit().putString("is_agent","").commit();
                sharedPreferences.edit().putString("vip_expire","").commit();
                sharedPreferences.edit().putString("is_vip","").commit();
                Intent intent = new Intent(context, LoginActivity.class);
                context.startActivity(intent);
                sharedPreferences.edit().putString("isRestarApp","true").commit();
                ActivityCollector.finishAll();

            }
        });

        mPopupWindowexit = new PopupWindow(popupWindow, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
        int mPopupWindowWidth = mPopupWindowexit.getWidth();
        int mPopupWindowHeight = mPopupWindowexit.getHeight();
        // 需要设置一下此参数，点击外边可消失
        mPopupWindowexit.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindowexit.setOutsideTouchable(true);
        mPopupWindowexit.setTouchable(true);
        mPopupWindowexit.setFocusable(true); // pop 显示时， 不让外部 view 响应点击事件
        //防止被底部虚拟键挡住
        mPopupWindowexit.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        if (Build.VERSION.SDK_INT >= 24) { // Android 7.x中,PopupWindow高度为match_parent时,会出现兼容性问题,需要处理兼容性
            int Screenheightth = activity.getWindowManager().getDefaultDisplay().getHeight();
            mPopupWindowexit.showAtLocation(view,
                    Gravity.BOTTOM,
                    0,
                    Screenheightth / 2);
//            Log.e("底部导航栏高度", utils.getNavigationBarHeight(context) + "    gaodu");

        } else {
            int Screenheightth = activity.getWindowManager().getDefaultDisplay().getHeight();
            mPopupWindowexit.showAtLocation(view,
                    Gravity.BOTTOM,
                    0,
                    0);
        }
        mPopupWindowexit.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });

    }

}
